package com.abso.mp3tunes.locker.ui.properties;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.abso.mp3tunes.locker.core.data.LockerDataUtils;
import com.abso.mp3tunes.locker.core.data.Track;

/**
 * A specialized section showing the properties of a track.
 */
public class TrackPropertySection extends AbstractLockerPropertySection {

	/** The text control used to show the track's title. */
	private Text titleText;

	/** The text control used to show the track's number. */
	private Text numberText;

	/** The text control used to show the track's time length. */
	private Text lengthText;

	/** The text control used to show the track's size. */
	private Text sizeText;

	/** The text control used to show the album's title. */
	private Text albumTitleText;

	/** The text control used to show the album's year. */
	private Text albumYearText;

	/** The text control used to show the artist's name. */
	private Text artistNameText;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite,
	 *      org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	public void createControls(Composite parent,
			TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);
		Composite mainComposite = createMainControl(parent);
		createCLabel(mainComposite, "Title:");
		titleText = createText(mainComposite, false);
		createCLabel(mainComposite, "Number:");
		numberText = createText(mainComposite, false);
		createCLabel(mainComposite, "Length:");
		lengthText = createText(mainComposite, false);
		createCLabel(mainComposite, "Size:");
		sizeText = createText(mainComposite, false);
		createCLabel(mainComposite, "Album:");
		albumTitleText = createText(mainComposite, false);
		createCLabel(mainComposite, "Album Year:");
		albumYearText = createText(mainComposite, false);
		createCLabel(mainComposite, "Artist:");
		artistNameText = createText(mainComposite, false);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#refresh()
	 */
	public void refresh() {
		List elems = getSelectedElements();
		if (elems.size() == 1) {
			Track track = (Track) elems.get(0);
			titleText.setText(track.getTitle());
			numberText.setText(Integer.toString(track.getNumber()));
			lengthText.setText(LockerDataUtils.getPrettyPrintedLength(track
					.getLength()));
			sizeText.setText(LockerDataUtils.getPrettyPrintedSize(track
					.getFileSize()));
			albumTitleText.setText(track.getAlbumTitle());
			albumYearText.setText(track.getAlbumYear());
			artistNameText.setText(track.getArtistName());
		} else {
			Set artistNames = new HashSet();
			Set albumTitles = new HashSet();
			Set albumYears = new HashSet();
			double length = 0.0d;
			long size = 0L;
			for (Iterator i = elems.iterator(); i.hasNext();) {
				Track track = (Track) i.next();
				artistNames.add(track.getArtistName());
				albumTitles.add(track.getAlbumTitle());
				albumYears.add(track.getAlbumYear());
				length += track.getLength();
				size += track.getFileSize();
			}
			titleText.setText(elems.size() + " tracks");
			numberText.setText("-");
			lengthText.setText(LockerDataUtils.getPrettyPrintedLength(length));
			sizeText.setText(LockerDataUtils.getPrettyPrintedSize(size));
			if (albumTitles.size() == 1) {
				albumTitleText.setText((String) albumTitles.iterator().next());
			} else {
				albumTitleText.setText(albumTitles.size() + " albums");
			}
			if (albumYears.size() == 1) {
				albumYearText.setText((String) albumYears.iterator().next());
			} else {
				albumYearText.setText("-");
			}
			if (artistNames.size() == 1) {
				artistNameText.setText((String) artistNames.iterator().next());
			} else {
				artistNameText.setText(artistNames.size() + " artists");
			}
		}
	}

}
